﻿#include "XmlItem.h"
#include <iostream>
#include <new>      // yes, this one new style header, is in the Android SDK.
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
#   include <stddef.h>
#   include <stdarg.h>
#else
#   include <cstddef>
#   include <cstdarg>
#endif

#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
static inline int TIXML_SNPRINTF(char* buffer, size_t size, const char* format, ...)
{
    va_list va;
    va_start(va, format);
    int result = vsnprintf_s(buffer, size, _TRUNCATE, format, va);
    va_end(va);
    return result;
}

static inline int TIXML_VSNPRINTF(char* buffer, size_t size, const char* format, va_list va)
{
    int result = vsnprintf_s(buffer, size, _TRUNCATE, format, va);
    return result;
}

#define TIXML_VSCPRINTF _vscprintf
#define TIXML_SSCANF    sscanf_s
#elif defined _MSC_VER
// Microsoft Visual Studio 2003 and earlier or WinCE
#define TIXML_SNPRINTF  _snprintf
#define TIXML_VSNPRINTF _vsnprintf
#define TIXML_SSCANF    sscanf
#if (_MSC_VER < 1400 ) && (!defined WINCE)
    // Microsoft Visual Studio 2003 and not WinCE.
#define TIXML_VSCPRINTF   _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have.
#else
    // Microsoft Visual Studio 2003 and earlier or WinCE.
static inline int TIXML_VSCPRINTF(const char* format, va_list va)
{
    int len = 512;
    for (;;) {
        len = len * 2;
        char* str = new char[len]();
        const int required = _vsnprintf(str, len, format, va);
        delete[] str;
        if (required != -1) {
            TIXMLASSERT(required >= 0);
            len = required;
            break;
        }
    }
    TIXMLASSERT(len >= 0);
    return len;
}
#endif
#else
// GCC version 3 and higher
//#warning( "Using sn* functions." )
#define TIXML_SNPRINTF  snprintf
#define TIXML_VSNPRINTF vsnprintf
static inline int TIXML_VSCPRINTF(const char* format, va_list va)
{
    int len = vsnprintf(0, 0, format, va);
    TIXMLASSERT(len >= 0);
    return len;
}
#define TIXML_SSCANF   sscanf
#endif

XmlItem::XmlItem()
{

}

/*********************************
@函数描述:析构函数
@入参 :void
@返回值 :void
**********************************/
XmlItem::~XmlItem()
{
}

void XmlItem::pushXmlHeader(const std::string& version, const std::string& encoding)
{
    //添加XML头结构
    Print("<?xml version=\"%s\" encoding=\"%s\"?>", version.c_str(), encoding.c_str());
}

tinyxml2::XMLElement* XmlItem::getXmlChildeEleByName(tinyxml2::XMLElement* parentElement, const std::string& memberName)
{
    //通过名称获取xml的member
    for (tinyxml2::XMLElement* element = parentElement->FirstChildElement();
        NULL != element;
        element = element->NextSiblingElement())
    {
        if (NULL == element->Name())
        {
            continue;
        }
        //根据名称获取node
        if (memberName == element->Name())
        {
            return element;
        }
    }
    return NULL;
}

tinyxml2::XMLAttribute* XmlItem::getXmlChildAttrByName(tinyxml2::XMLElement* element, const std::string& memberName)
{
    //通过名称获取xml的member
    for (auto attr = element->FirstAttribute();
        NULL != attr;
        attr = attr->Next())
    {
        if (NULL == attr->Name())
        {
            continue;
        }
        //根据名称获取属性
        if (memberName == attr->Name())
        {
            return const_cast<tinyxml2::XMLAttribute*>(attr);
        }
    }
    return NULL;
}

